package com.fernfx.xingtan.my.presenter;

import android.content.Context;
import android.view.View;

import com.fernfx.xingtan.common.AppManager;
import com.fernfx.xingtan.common.base.BaseApplication;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseIRequestCallback;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.my.contract.SettingApplicationContract;
import com.fernfx.xingtan.my.model.SettingApplicationModel;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.user.ui.LoginActivity;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.GlideUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.view.dialog.AlertDialog;

import java.util.Map;

/**
 * Created by admin on 2018/8/16.
 */

public class SettingApplicationPresenter<P extends SettingApplicationContract.View> implements SettingApplicationContract.Presenter {
    private P P;
    private SettingApplicationContract.Model model;

    @Override
    public void init(BaseView view) {
        this.P = (P) view;
        model = new SettingApplicationModel();
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {

    }

    @Override
    public void detachView() {
        if (model != null) {
            model = null;
        }
    }

    @Override
    public void showClearCacheDialog(Context context) {
        final AlertDialog alertDialog = new AlertDialog(context)
                .builder()
                .setTitle("确定要清除缓存吗？")
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //清除缓存并删除
                        GlideUtil.clearImageAllCache();
                        P.freshenCacheImg();
                    }
                })
                .setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });
        alertDialog.show();
    }

    @Override
    public void quitLogin(Context context) {
        final AlertDialog alertDialog = new AlertDialog(context)
                .builder()
                .setTitle("确定要退出登录吗？")
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //退出登录操作
                        BaseApplication.memberEntity = null;
                        AccountManager.clearLoginUserInfo();
                        AccountManager.saveLoginStatus(false);
                        //关闭所有页面跳转到登录页面
                        AppManager.getAppManager().finishAllActivity();
                        LoginActivity.start(P.getActivity());
                    }
                })
                .setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });
        alertDialog.show();
    }

    @Override
    public void getHelpUrl(Map<String, Object> requestArgsMap) {
        model.getHelpUrl(requestArgsMap, new BaseIRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                String message = result.getMessage();
                BaseEntity baseEntity = FastJsonUtil.fromBean(message, BaseEntity.class);
                if (OtherUtil.checkRequestStatus(baseEntity)) {
                    P.setHelpUrl(baseEntity.getMsg());
                }
            }
        });
    }

    @Override
    public void getAboutUsUrl(final Map<String, Object> requestArgsMap) {
        model.getAboutUsUrl(requestArgsMap, new BaseIRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                String message = result.getMessage();
                BaseEntity baseEntity = FastJsonUtil.fromBean(message, BaseEntity.class);
                if (OtherUtil.checkRequestStatus(baseEntity)) {
                    P.setAboutUsUrl(baseEntity.getMsg());
                }
            }
        });
    }

}
